# 进程与线程
import os
from multiprocessing import Process

# only works on Unix/Linux/Mac:
# print('Process (%s) start...' % os.getpid())

# pid = os.fork()                 # 复制当前进程(父进程)为子进程
#                                 # 注意：该函数返回两次，子进程返回值为0，父进程返回子进程ID
# if pid == 0:                    # 子进程
#     print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))   # 子进程调用getpid拿到父进程ID
# else:                           # 父进程
#     print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

def run_proc(name):
    print('Run child process %s (%s)' % (name, os.getpid()))

if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    P = Process(target = run_proc, args = ('test',))    # 创建子进程时，只需要传入一个执行函数和函数的参数
    print('Child process will start')
    P.start()
    P.join()                                            # 等待子进程结束后继续运行，用于进程同步
    print('Child process end.')